/**
 * @Description: 模块/子系统及主页面页面快速生成脚本
 */
const fs = require('fs');
const path = require('path');

const basePath = path.resolve(__dirname, '../src');

const moduleName = process.argv[2]; // 获取第一个命令行参数
const capsModuleName = moduleName.substring(0, 1).toUpperCase() + moduleName.substring(1);
if (!moduleName) {
  console.log('需要输入待创建模块或子系统名称');
  console.log('示例：npm run tpl module');
  process.exit(0);
}

/**
 * @msg: vue页面模板
 */
const vueTemplate = `<template>
  <div class="index-wrap">
    {{data.moduleName}}
  </div>
</template>

<script lang="ts" src="./index.ts"></script>

<style lang="scss" scoped>
  @import './_index.scss'
</style>

`;

// ts模板
const tsTemplate = `import store from '@/store';
import ${moduleName} from '@/views/${moduleName}/store';
import { getModule } from 'vuex-module-decorators';
import { Component, Vue } from "vue-property-decorator";
import { IndexData } from '@/views/${moduleName}/interface';

const ${capsModuleName} = getModule(${moduleName}, store);

@Component({})
export default class Index extends Vue {

  private data: IndexData = {
    moduleName: '${moduleName}'
  };

  private created() {
  }

  private activated() {
  }

  private mounted() {
  }

  // 初始化函数
  private init() {
  }

}

`;

// scss模版
const scssTemplate = `@import "@/assets/style/variables.scss";

.index-wrap {
  width: 100%;
}

`;

// interface模版
const interfaceTemplate = `// index.Data 参数类型
export interface IndexData {
  moduleName: string;
}

`;

// router模版
const routerTemplate = `export default [
];

`;

// vuex模版
const vuexTemplate = `import { Module, VuexModule, Mutation, Action } from 'vuex-module-decorators';
import * as ${capsModuleName}Service from '@/views/${moduleName}/${moduleName}-service';

@Module({ name: '${capsModuleName}', namespaced: true, stateFactory: true })
export default class ${capsModuleName} extends VuexModule {

  public data: any = {
    author: undefined,
  }

  get getAuthor() {
    return this.data.author;
  }

  @Mutation
  private updateState(data: any) {
    for (const key of Object.keys(data)) {
      if (!this.data.hasOwnProperty(key)) { return }
      this.data[key] = data[key];
    }
  }

  @Action({ commit: 'updateState' })
  public async updateStateAsync() {
  }

};

`;

// service接口模版
const serviceTempalte = `import Vue from "vue";

export const test = (params) => {
}

`;

fs.mkdirSync(`${basePath}/views/${moduleName}`);
fs.mkdirSync(`${basePath}/views/${moduleName}/index`);

process.chdir(`${basePath}/views/${moduleName}/index`);
fs.writeFileSync(`index.vue`, vueTemplate);
fs.writeFileSync(`index.ts`, tsTemplate);
fs.writeFileSync(`_index.scss`, scssTemplate);

process.chdir(`${basePath}/views/${moduleName}`);
fs.writeFileSync(`router.ts`, routerTemplate);
fs.writeFileSync(`store.ts`, vuexTemplate);
fs.writeFileSync(`interface.ts`, interfaceTemplate);
fs.writeFileSync(`${moduleName}-service.ts`, serviceTempalte);

process.exit(0)
